﻿using ZHDJ.MobileInterface.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZHDJ.MobileInterface
{
    public partial class JavaJiami : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //this.TextBox1.Text = EncryptWithJava(txt1.Text.Trim(), TextBox2.Text.Trim().PadRight(24, 'a'));
            this.TextBox1.Text = JavaDES.Encode(txt1.Text.Trim(), TextBox2.Text.Trim().PadRight(24, 'a'), TextBox3.Text.Trim());
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //this.TextBox1.Text = DecryptWithJava(txt1.Text.Trim().ToLower(), TextBox2.Text.Trim().PadRight(24, 'a'));
            this.TextBox1.Text = JavaDES.Decode(txt1.Text.Trim().ToLower(), TextBox2.Text.Trim().PadRight(24, 'a'), TextBox3.Text.Trim());
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            //this.TextBox1.Text = DecryptWithJava(txt1.Text.Trim().ToLower(), "TH2djtsg".PadRight(24, 'a'));
            this.TextBox1.Text = JavaDES.Decode(txt1.Text.Trim().ToLower(), "TH2djtsg", TextBox3.Text.Trim());
        } 
        public string EncryptWithJava(string str, string key)
        {
            if (key.Length < 8 || string.IsNullOrEmpty(str))
            {
                throw new Exception("加密key小于8或者加密字符串为空！");
            }
            byte[] bKey = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] bIV = { 0x01 };
            byte[] bStr = Encoding.UTF8.GetBytes(str);
            try
            {
                DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
                desc.Padding = PaddingMode.PKCS7;//补位  
                desc.Mode = CipherMode.ECB;//此模式，对加密，IV无意义  
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, desc.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
                    {
                        cStream.Write(bStr, 0, bStr.Length);
                        cStream.FlushFinalBlock();
                        StringBuilder ret = new StringBuilder();
                        byte[] res = mStream.ToArray();
                        //return UTF8Encoding.UTF8.GetString(res);
                        foreach (byte b in res)
                        {
                            ret.AppendFormat("{0:x2}", b);
                        }
                        //return UTF8Encoding.UTF8.GetString(res);
                        return ret.ToString().ToUpper();
                    }
                }
            }
            catch
            {
                return string.Empty;
            }
        }

        public string DecryptWithJava(string str, string key)
        {
            if (key.Length < 8 || string.IsNullOrEmpty(str))
            {
                throw new Exception("加密key小于8或者加密字符串为空！");
            }

            byte[] destIVBytes = new byte[8];
            System.Array.Copy(UTF8Encoding.UTF8.GetBytes("01234567"), 0, destIVBytes, 0, 8);
            byte[] bKey = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] bIV = { 0x01 };
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                des.Padding = PaddingMode.PKCS7;//补位  
                des.Mode = CipherMode.ECB;//此模式，对加密，IV无意义
                byte[] inputByteArray = new byte[str.Length / 2];
                for (int x = 0; x < str.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(str.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                //des.Key = bKey;
                //des.IV = bIV;
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(bKey,bIV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                StringBuilder ret = new StringBuilder();

                return System.Text.Encoding.Default.GetString(ms.ToArray());

            }
            catch (Exception ex)
            {
                TextBox1.Text = ex.Message;
                return "";
            }
        }

        public static string ToHex(string s)
        {
            if ((s.Length % 2) != 0)
            {
                s += " ";//空格 
                //throw new ArgumentException("s is not valid chinese string!"); 
            }
            System.Text.Encoding chs = Encoding.UTF8;
            byte[] bytes = chs.GetBytes(s);
            string str = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                str += string.Format("{0:X}", bytes[i]);
               
            }
            return str.ToLower();
        }
    }
}